#include "ComponentRenderable.h"


//#include "Ogre.h"
#include "../OgreFrameWork.h"

#include "ComponentPosition.h"

#include "../constant.h"
#include "../maths.h"

#include "../Object.h"

ComponentRenderable::ComponentRenderable(ComponentManager* _parent, ComponentArguments _args):
    Component(_parent, _args)
{
    //ctor
    Ogre::SceneManager *sceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->getSceneManager("GameSceneMgr");

    OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentRenderable::ComponentRenderable() !   nameMesh = " + _args.getArgument<std::string>(std::string("nameMesh")));

    m_node = sceneMgr->getRootSceneNode()->createChildSceneNode(((Object*)m_parent)->getName());
    m_entity = sceneMgr->createEntity(std::string("entity_")+((Object*)m_parent)->getName()+Ogre::StringConverter::toString(randInt(999,999999)), _args.getArgument<std::string>(std::string("nameMesh")));

    m_nameMesh = std::string(_args.getArgument<std::string>(std::string("nameMesh")));
    m_node->attachObject(m_entity);
    m_node->setInheritOrientation(false);



      if (_args.hasArgument("scale"))
    {
        m_node->setScale(_args.getArgument<Ogre::Vector3>("scale"));
    }else
        m_node->setScale(Ogre::Vector3(0.01,0.01,0.01));

    if (_args.hasArgument("rotate"))
    {
        m_baseRotation = _args.getArgument<Ogre::Vector3>("rotate");
    }



    if (_args.hasArgument("offset"))
    {
        //m_entity->setPosition(_args.getArgument<Ogre::Vector3>("offset"));
    }else



    if (_args.hasArgument("material"))
    {
        m_entity->setMaterialName(_args.getArgument<std::string>("material"));
    }else
    m_entity->setMaterialName("Examples/monsterSkin");




/*
    m_positionComponent=0;
    if (m_parent->hasComponent("Position"))
    {
        m_positionComponent = m_parent->getComponent("Position");
        m_node->setPosition(((ComponentPosition*)m_parent->getComponent("Position"))->getPosition());


        OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentRenderable::ComponentRenderable() set the position of the object  x="+
                                                                 Ogre::StringConverter::toString(m_node->getPosition().x)+
                                                                 "  y="+Ogre::StringConverter::toString(m_node->getPosition().z));
                                                                 */
       // update(FIRST_STEP_TIME);



}

ComponentRenderable::~ComponentRenderable()
{
    //dtor
}



/** @brief registerNewComponent
  *
  * @todo: document this function
  */
void ComponentRenderable::registerNewComponent(std::string _newComponentName)
{

}

/** @brief update
  *
  * @todo: document this function
  */
void ComponentRenderable::update(double timeSinceLastFrame)
{


  //  m_node->setPosition(((ComponentPosition*)m_parent->getComponent("Position"))->getPosition());




}

/** @brief loadFromXML
  *
  * @todo: document this function
  */
void ComponentRenderable::loadFromXML(TiXmlElement* parentElement)
{

}

/** @brief saveToXML
  *
  * @todo: document this function
  */
void ComponentRenderable::saveToXML(TiXmlElement* parentElement)
{
    TiXmlElement* element = new TiXmlElement("mesh");
    parentElement->LinkEndChild(element);
    element->SetAttribute("name", m_nameMesh.c_str());

}

/** @brief destroy
  *
  * @todo: document this function
  */
void ComponentRenderable::destroy()
{

}

/** @brief initialise
  *
  * @todo: document this function
  */
void ComponentRenderable::initialize()
{

}


/** @brief facePoint
  *
  * @todo: document this function
  */
void ComponentRenderable::facePoint(const Ogre::Vector3 &point)
{

    Ogre::Vector3 direction =  point - m_node->getPosition();
    direction.y=0;
    direction.normalise();



    Ogre::Vector3 src = m_node->getOrientation() * Ogre::Vector3::UNIT_X;
    if ((1.0f + src.dotProduct(direction)) < 0.0001f)
    {
        m_node->yaw(Ogre::Degree(180));
    }
    else
    {
        Ogre::Quaternion quat = src.getRotationTo(direction);
        m_node->rotate(quat);
    }
    m_node->yaw(Ogre::Degree(180));
    m_node->yaw(Ogre::Radian(Ogre::Degree(m_baseRotation.y)), Ogre::Node::TS_LOCAL);
}
